﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.System;

namespace WMS.FC.Infrastructure.Filter
{
    public class FilterBase : ActionFilterAttribute
    {
        protected readonly ILogger<FilterBase> logger;
        protected readonly IOptions<WmsSettings> options;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="options"></param>
        public FilterBase(ILogger<FilterBase> logger, IOptions<WmsSettings> options)
        {
            this.logger = logger;
            this.options = options;
        }

        /// <summary>
        /// 鉴权失败
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message"></param>
        protected void FailAuthority(ActionExecutingContext context, string message)
        {
            var response = new ResponseBase()
            {
                Code = ErrorCode.E002.ToString(),
                Message = message
            };

            context.Result = new JsonResult(response);
        }

        /// <summary>
        /// 拦截器异常
        /// </summary>
        /// <param name="context"></param>
        /// <param name="ex"></param>
        protected void FilterException(ActionExecutingContext context, Exception ex)
        {
            var response = new ResponseBase()
            {
                Code = ErrorCode.E006.ToString(),
                Message = ex.Message
            };

            context.Result = new JsonResult(response);
        }
    }
}
